package com.owen.springboot.smartparkingms.common.model;

import java.util.HashMap;
import java.util.Map;

/**
 *  页面响应
 */
public class Result extends HashMap<String, Object> {

	private static final long serialVersionUID = 1L;

	/**
	 * 默认成功消息0
	 */
	public Result() {
		put("code", 0);
	}

	/**
	 * 默认错误消息500
	 */
	public static Result error() {
		return error(500, "未知异常，请联系管理员");
	}

	/**
	 * 错误消息500
	 * @param msg
	 * @return
	 */
	public static Result error(String msg) {
		return error(500, msg);
	}

	/**
	 * 错误消息
	 * @param code
	 * @param msg
	 * @return
	 */
	public static Result error(int code, String msg) {
		Result r = new Result();
		r.put("code", code);
		r.put("msg", msg);
		return r;
	}

	/**
	 * 成功消息
	 * @param msg
	 * @return
	 */
	public static Result ok(Object msg) {
		Result r = new Result();
		r.put("msg", msg);
		return r;
	}

	/**
	 * 成功消息
	 * @param msg
	 * @param data
	 * @return
	 */
    public static Result ok(Object msg,Object data) {
        Result r = new Result();
        r.put("msg", msg);
        r.put("data", data);
        return r;
    }

	/**
	 * 成功消息
	 * @param map
	 * @return
	 */
	public static Result ok(Map<String, Object> map) {
		Result r = new Result();
		r.putAll(map);
		return r;
	}

	/**
	 * 默认成功消息
	 * @return
	 */
	public static Result ok() {
		Result r = new Result();
		r.put("msg", "操作成功");
		return r;
	}

	/**
	 * 判断消息是否为成功消息
	 * @param r
	 * @return
	 */
    public static Boolean isOk(Result r) {
		return r.get("code").toString().equals("0");
	}

	/**
	 * 额外添加数据
	 * @param key
	 * @param value
	 * @return
	 */
	@Override
	public Result put(String key, Object value) {
		super.put(key, value);
		return this;
	}
}